package Controller;

import Data.*;
import MyInterfaces.MassageType;
import Service.Main;
import Service.SendMassage;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;

public class VerifyFriends {

    @FXML
    private Button ensureButton;

    @FXML
    private Button refuseButton;

    @FXML
    private Label id;

    @FXML
    private ImageView avatar;

    //private Label content;

    @FXML
    private Button ensureGroupButton;


    @FXML
    private Button refuseGroupButton;

    @FXML
    private PrivateInformationData privateInformationData;

    public PrivateInformationData getPrivateInformationData() {
        return privateInformationData;
    }

    private GroupData groupData;

    private GroupFriendData groupFriendData;

    private void setGroupData()
    {
        ChatMain chatMain=Main.AllContronller.getChatMainController();
        ArrayList<GroupData> arrayList=chatMain.myGroupArry;
        for (int i = 0; i < arrayList.size(); i++) {
            if(groupData.getGroup_id().equals(arrayList.get(i).getGroup_id()))
            {
                //如果是
                this.groupData=arrayList.get(i);
            }
        }
    }

    public void setGroupVerify(GroupFriendData groupFriendData, GroupData groupData)
    {
        this.groupData=groupData;
        this.groupFriendData=groupFriendData;
        //需要知道的一点是
        Image image=new Image(new ByteArrayInputStream(groupFriendData.getAvatar()));
        Circle circle=new Circle(75,75,75);
        avatar.setFitHeight(150);
        avatar.setFitWidth(150);
        avatar.setImage(image);
        avatar.setClip(circle);

        id.setText("id为"+groupFriendData.getId());

        ensureButton.setVisible(false);
        ensureButton.setDisable(true);

        refuseButton.setVisible(false);
        refuseButton.setDisable(true);
    }

    public void setPrivateInformationData(PrivateInformationData privateInformationData) {
        this.privateInformationData = privateInformationData;
        Image image=new Image(new ByteArrayInputStream(privateInformationData.getAvatar()));
        Circle circle=new Circle(75,75,75);
        avatar.setFitHeight(150);
        avatar.setFitWidth(150);
        avatar.setImage(image);
        avatar.setClip(circle);
        id.setText("id为"+privateInformationData.getUserid());

        ensureGroupButton.setVisible(false);
        ensureGroupButton.setDisable(true);

        refuseGroupButton.setVisible(false);
        refuseGroupButton.setDisable(true);
    }

    @FXML
    void ensureAction(ActionEvent event) throws IOException {
        //同意加对方为好友
        ChatMain chatMain= Main.AllContronller.getChatMainController();
        PrivateInformationData mydata=chatMain.getMydata();
        ChatData chatData=new ChatData(null,null,mydata.getUserid(),privateInformationData.getUserid());

        event massage=new event();
        massage.setType(MassageType.Agree_Add_Friend);
        massage.setT(chatData);

        new SendMassage().sendMassage(massage);

        Stage stage=(Stage) ensureButton.getScene().getWindow();
        stage.close();
    }

    @FXML
    void refuseAction(ActionEvent event) throws IOException {
        //拒绝好友请求
        ChatMain chatMain= Main.AllContronller.getChatMainController();
        PrivateInformationData mydata=chatMain.getMydata();
        ChatData chatData=new ChatData(null,null,mydata.getUserid(),privateInformationData.getUserid());

        event massage=new event();
        massage.setType(MassageType.Refuse_Add_Friend);
        massage.setT(chatData);

        new SendMassage().sendMassage(massage);

        Stage stage=(Stage) refuseButton.getScene().getWindow();
        stage.close();
    }
    @FXML
    void loadInformation(MouseEvent event) throws IOException {
        //加载个人信息,点击头像出现个人信息
        Stage stage=new Stage();
        File file=new File("E:\\JDK\\QQClient\\nox\\src\\fxml\\PrivateInformation.fxml");
        URL location=file.toURI().toURL();
        FXMLLoader fxmlLoader=new FXMLLoader();
        fxmlLoader.setLocation(location);
        Parent root=fxmlLoader.load();
        stage.setTitle(privateInformationData.getUserNickName()+"的个人信息");
        stage.setResizable(false);
        stage.setScene(new Scene(root,400,600));
        stage.show();
        PrivateInformation privateInformation=fxmlLoader.getController();
        privateInformation.setFriend(privateInformationData);
    }


    public void ensureGroupAction(ActionEvent actionEvent) throws IOException {
        //需要发送好友的群聊，好友的信息，然后加入到群聊当中
        //所有人的列表都需要在加一个人，之前邀请好友的时候，是3就是加了一个人可以直接调用之前的函数
        //不是3就是重新设置这个人的identity即可
        //先判断好友列表是否已经有了这个人且身份不为0
        setGroupData();
        ArrayList<GroupFriendData> arry=groupData.getGroup_friend_list();
        for (int i = 0; i < arry.size(); i++) {
            if(arry.get(i).getId().equals(groupFriendData.getId()))
            {
                //如果id相同判断是否，身份已经变了
                if(arry.get(i).getIdentity()!=0)
                {
                    Alert alert = new Alert(Alert.AlertType.INFORMATION);
                    alert.setTitle("错误");
                    alert.setHeaderText(null);
                    alert.setContentText("此人已经在列表中，不需要验证!");
                    alert.showAndWait();

                    Stage stage=(Stage) ensureGroupButton.getScene().getWindow();
                    stage.close();
                    return ;
                }
            }
        }
        System.out.println("此时发送同意好友的消息");

        GroupOperationData groupOperationData=new GroupOperationData();
        groupFriendData.setGroup_id(groupData.getGroup_id());
        groupOperationData.setReceiver(groupFriendData);
        groupOperationData.setFriendlist(groupData.getGroup_friend_list());

        event massage=new event();
        massage.setType(MassageType.Agree_Group_Friend);
        massage.setT(groupOperationData);

        new SendMassage().sendMassage(massage);

        Stage stage=(Stage) ensureGroupButton.getScene().getWindow();
        stage.close();
    }

    public void refuseGroupAction(ActionEvent actionEvent) throws IOException {
        //拒绝的话，就删掉，所有的，服务器需要筛选，如果为1，2的就不显示这个人的信息了
        //主要是在群主和管理员这里

        ArrayList<GroupFriendData> arry=groupData.getGroup_friend_list();
        for (int i = 0; i < arry.size(); i++) {
            if(arry.get(i).getId().equals(groupFriendData.getId()))
            {
                //如果id相同判断是否，身份已经变了
                if(arry.get(i).getIdentity()!=0)
                {
                    Alert alert = new Alert(Alert.AlertType.INFORMATION);
                    alert.setTitle("错误");
                    alert.setHeaderText(null);
                    alert.setContentText("此人已经在列表中，不需要验证!");
                    alert.showAndWait();

                    Stage stage=(Stage) refuseGroupButton.getScene().getWindow();
                    stage.close();
                    return ;
                }
            }
        }

        System.out.println("开始发送拒绝好友申请的消息");

        GroupOperationData groupOperationData=new GroupOperationData();
        groupOperationData.setReceiver(groupFriendData);
        groupOperationData.setFriendlist(groupData.getGroup_friend_list());

        event massage=new event();
        massage.setType(MassageType.Refuse_Group_Friend);
        massage.setT(groupOperationData);

        new SendMassage().sendMassage(massage);

        Stage stage=(Stage) refuseGroupButton.getScene().getWindow();
        stage.close();
    }
}
